layui.use(['form','layer'],function (){
    let form = layui.form,layer = layui.layer;
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 初始化回显新增信息
*/
$(function (){
    //初始化单位信息
    initUserInfoDwCode();
    //初始化部门信息
    initUserInfoDep();
    //初始化角色信息
    initUserInfoRole();
});

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 放弃按钮
*/
function quitInsertUserInfo() {
    //获取当前弹窗的索引，并关闭；parent 是 layui 定义的全局变量
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 新增用户信息
*/
function insetUserInfo() {
    //账号
    let urNm = $("#urNm").val().trim();
    //密码
    let urPwd = $("#urPwd").val();
    //姓名
    let urRealNm = $("#urRealNm").val().trim();
    //年龄
    let urAge = $("#urAge").val().trim();
    //性别
    let urSex = $("input[name = 'urSex']:checked").val();
    //地址
    let urAds = $("#urAds").val().trim();
    //电话
    let urTel = $("#urTel").val().trim();
    //单位
    let urDwCode = $("#urDwCode").val().trim();
    //部门
    let urBmCode = $("select[name = 'urBmCode']").val();
    //角色
    let roId = $("select[name = 'roId']").val();
    //备用
    let urResv = $("#urResv").val().trim();

    if (valueIsEmpty(urNm) || valueIsEmpty(urPwd)){
        layer.msg("账号或密码不能为空!",{icon:0,time: 1500});
        return;
    }
    if (valueIsEmpty(urBmCode)){
        layer.msg("部门不能为空!",{icon:0,time: 1500});
        return;
    }
    if (valueIsEmpty(roId)){
        layer.msg("角色不能为空!",{icon:0,time: 1500});
        return;
    }
    let userJsonData = {
        "urNm" : urNm,
        "urPwd" : urPwd,
        "urRealNm" : urRealNm,
        "urAge" : urAge,
        "urSex" : urSex,
        "urAds" : urAds,
        "urTel" : urTel,
        "urDwCode" : urDwCode,
        "urBmCode" : urBmCode,
        //角色id userInfoVO 里的 roleInfoVO 的 roId
        "roleInfoVO.roId" : roId,
        "urResv" : urResv
    }
    sendAjax("/systemDefend/userAdmin/insertUserInfo",userJsonData,function (result){
        if (valueIsEqual(result.code,"00")){
            quitInsertUserInfo();
        }else if (valueIsEqual(result.code,"02")){
            layer.msg(result.data,{icon:5,time: 1500});
        }
    });
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/14
* @description: 初始化用户单位信息
*/
function initUserInfoDwCode() {
    sendAjax("/systemDefend/userAdmin/initUserInfoDwCode",null,function (result) {
        if (valueIsEqual("00",result.code)){
            let gvmtTypeInf = result.data;
            $("#urDwCode").val(gvmtTypeInf.gtTypeCode);
            $("#urDwNm").val(gvmtTypeInf.gtTypeNm);
        }
    })
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/11
* @description: 初始化返显部门信息
*/
function initUserInfoDep() {
    sendAjax("/systemDefend/userAdmin/initUserInfoDep",null,function (result) {
        if (valueIsEqual("00",result.code)){
            let deptDescList = result.data;
            let html = "";
            deptDescList.forEach(function (item,index) {
                html += "<option value = '"+item.dtTypeCode+"'>" +item.dtTypeNm+ "</option>";
            })
            $("select[name='urBmCode']").append(html);
            layui.form.render('select');
        }
    })
}

/**
 * @author: ChengJW
 * @params:
 * @return:
 * @date: 2021/11/11
 * @description: 初始化返显角色信息
 */
function initUserInfoRole() {
    sendAjax("/systemDefend/userAdmin/initUserInfoRole",null,function (result) {
        if (valueIsEqual("00",result.code)){
            let roleDescList = result.data;
            let html = "";
            roleDescList.forEach(function (item,index) {
                html += "<option value = '"+item.roId+"'>" +item.roDesc+ "</option>";
            })
            $("select[name='roId']").append(html);
            layui.form.render('select');
        }
    });
}
